import pyscreeze
from loguru import logger

import Config
from driver.driver import *
from public import public
from public.public import get_soldier_cities, get_sleep_time, get_solider_cities_reserve_config

SLEEP_TIME = 1


def _set_commander_free(left, top):
    """
    将领设置为空闲状态
    :param left:
    :param top:
    :return:
    """
    _ = Config.ChengNeiConfig.ZhaoXianGuan
    mouse_move_to_and_click(left + _[0], top + _[1])

    _ = Config.ZhaoXianGuan.RenMing1
    mouse_move_to_and_click(left + _[0], top + _[1])

    time.sleep(SLEEP_TIME)

    commander_img = "./imgs/ReXueYongShi2.jpg"
    _ = pyautogui.locateOnScreen(commander_img, confidence=0.9)
    _ = pyscreeze.center(_)
    mouse_move_to_and_click(_[0] + 200, _[1])
    mouse_move_to_and_click(_[0] + 200, _[1] + 25)

    _ = Config.ZhaoXianGuan.RenMing2
    mouse_move_to_and_click(left + _[0], top + _[1])

    _ = Config.ZhaoXianGuan.CloseButton1
    mouse_move_to_and_click(left + _[0], top + _[1])
    time.sleep(2)


def _send_commander(left, top, x, y):
    """
    派遣将领
    :param left: App的left坐标
    :param top: App的top坐标
    :param x: 城池的x坐标
    :param y: 城池的y坐标
    :return:
    """
    public.send_commander(left, top, x, y)
    _ = Config.JiaoChang.ChiHouText
    mouse_move_to_and_click(left + _[0], top + _[1])
    clear_and_send_keys(1)
    _ = Config.JiaoChang.ChoseCommanderPullButton
    mouse_move_to_and_click(left + _[0], top + _[1])
    time.sleep(2)
    commander_img = "./imgs/ReXueYongShi3.jpg"
    _ = pyautogui.locateOnScreen(commander_img, confidence=0.9)
    _ = pyscreeze.center(_)
    mouse_move_to_and_click(_[0], _[1])
    _ = Config.JiaoChang.ChuZheng
    mouse_move_to_and_click(left + _[0], top + _[1])

    time.sleep(2)

    _ = Config.JiaoChang.CloseButton1
    mouse_move_to_and_click(left + _[0], top + _[1])

    time.sleep(2)


def _set_commander_on(left, top):
    """
    任命将领
    :param left: App的left坐标
    :param top: App的top坐标
    :return:
    """
    _ = Config.ChengNeiConfig.ZhaoXianGuan
    mouse_move_to_and_click(left + _[0], top + _[1])

    _ = Config.ZhaoXianGuan.RenMing1
    mouse_move_to_and_click(left + _[0], top + _[1])

    time.sleep(SLEEP_TIME)

    commander_img = "./imgs/ReXueYongShi2.jpg"
    _ = pyautogui.locateOnScreen(commander_img, confidence=0.9)
    _ = pyscreeze.center(_)
    mouse_move_to_and_click(_[0] + 200, _[1])
    mouse_move_to_and_click(_[0] + 200, _[1] + 90)

    _ = Config.ZhaoXianGuan.RenMing2
    mouse_move_to_and_click(left + _[0], top + _[1])

    _ = Config.ZhaoXianGuan.CloseButton1
    mouse_move_to_and_click(left + _[0], top + _[1])
    time.sleep(2)


def send_free_commander(left, top, x, y):
    """
    将招将派遣到相应的城池
    :param left: App的left坐标
    :param top: App的top坐标
    :param x: 城池的x坐标
    :param y: 城池的y坐标
    :return:
    """
    _ = Config.MainConfig.ChengNei
    mouse_move_to_and_click(left + _[0], top + _[1])
    _set_commander_free(left, top)
    _send_commander(left, top, x, y)
    public.entry_city(left, top, x, y, cityin=True)
    _ = Config.ChengNeiConfig.Commanders
    mouse_move_to_and_click(left + _[0], top + _[1])
    commander_img = "./imgs/ReXueYongShi1.jpg"
    while pyautogui.locateOnScreen(commander_img, confidence=0.9) is None:
        logger.info("将领尚未到达，5秒后再次寻找")
        time.sleep(5)
        # 刷新将领页面
        mouse_move_to_and_click(left + Config.ChengNeiConfig.Resource[0], top + Config.ChengNeiConfig.Resource[1])
        mouse_move_to_and_click(left + _[0], top + _[1])
    logger.info("将领已到达")
    _ = Config.ChengNeiConfig.Resource
    mouse_move_to_and_click(left + _[0], top + _[1])

    _set_commander_on(left, top)


def _init_barrack(left, top):
    """
    初始化兵营界面
    :param left: App的left坐标
    :param top: App的top坐标
    :return:
    """
    page_up = Config.Barrack.PageUp
    mouse_move_to(left + page_up[0], top + page_up[1])
    for _ in range(Config.Barrack.InitNum):
        click()
        time.sleep(0.1)


def check_double():
    """
    校验是否开启了双倍招兵
    :return:
    """
    img = "./imgs/SBZB.jpg"
    try:
        return pyautogui.locateOnScreen(img, confidence=0.98) is not None
    except BaseException:
        logger.error("双倍招兵校验错误，十秒重试")
        time.sleep(10)
        return False


def make_soldiers(left, top, soldier_type=Config.Barrack.QQPositionNum, num=2000, double=True, init_solders=True):
    """
    招兵
        1. 遍历每一座兵营；
        2. 初始化兵营界面；
        3. 点击轻骑兵的招募按钮；
        4. 输入招募参数并招募；
        5. 关闭当前兵营
    :param init_solders: 是否需要初始化
    :param left: App的left坐标
    :param top: App的top坐标
    :param soldier_type: 士兵类型
    :param num: 招募数量
    :param double: 是否双倍招募
    :return:
    """

    # 1. 遍历每一座兵营
    for index, _ in enumerate(Config.ChengNeiConfig.BarrackPositions):
        mouse_move_to_and_click(left + _[0], top + _[1])

        if init_solders:
            # 2. 初始化兵营界面
            if index == 0:
                _init_barrack(left, top)
                _ = Config.Barrack.PageDown
                mouse_move_to(left + _[0], top + _[1])
                for _ in range(soldier_type):
                    click()

        # 3. 点击轻骑兵的招募按钮
        _ = Config.Barrack.ZhaoMuButton
        mouse_move_to_and_click(left + _[0], top + _[1])

        # 4. 输入招募参数并招募
        if double:
            _ = Config.Barrack.DoubleButton
            # 校验双倍招兵
            while check_double() is False:
                mouse_move_to_and_click(left + _[0], top + _[1])
                mouse_move_to(left, top)
                time.sleep(SLEEP_TIME)
        _ = Config.Barrack.NumText
        mouse_move_to_and_click(left + _[0], top + _[1])

        clear_and_send_keys(num)

        _ = Config.Barrack.SureButton
        mouse_move_to_and_click(left + _[0], top + _[1])

        time.sleep(SLEEP_TIME)

        # 因为招募的时候经常出现卡顿的情况导致关闭按钮没有立即生效，所以这里需要加一个图片校验的步骤
        while pyautogui.locateOnScreen("./imgs/check_trainingTime.jpg", confidence=0.9) is not None:
            time.sleep(1)

        # 5. 关闭当前兵营
        _ = Config.Barrack.CloseButton
        mouse_move_to_and_click(left + _[0], top + _[1])

        while pyautogui.locateOnScreen("./imgs/check_training.jpg", confidence=0.9) is not None:
            time.sleep(1)


def main_make_soldiers(left, top, soldier_type=Config.Barrack.QQPositionNum, num=2000, double=True):
    """
    招兵
        1. 遍历每一座兵营；
        2. 初始化兵营界面；
        3. 点击轻骑兵的招募按钮；
        4. 输入招募参数并招募；
        5. 关闭当前兵营
    :param left: App的left坐标
    :param top: App的top坐标
    :param soldier_type: 士兵类型
    :param num: 招募数量
    :param double: 是否双倍招募
    :return:
    """
    # 1. 遍历每一座兵营
    for index, _ in enumerate(Config.ChengNeiConfig.BarrackPositions[4:]):
        mouse_move_to_and_click(left + _[0], top + _[1])

        # 2. 初始化兵营界面
        if index == 0:
            _init_barrack(left, top)
            _ = Config.Barrack.PageDown
            mouse_move_to(left + _[0], top + _[1])
            for _ in range(soldier_type):
                click()

        # 3. 点击轻骑兵的招募按钮
        _ = Config.Barrack.ZhaoMuButton
        mouse_move_to_and_click(left + _[0], top + _[1])

        # 4. 输入招募参数并招募
        if double:
            _ = Config.Barrack.DoubleButton
            # 校验双倍招兵
            while check_double() is False:
                mouse_move_to_and_click(left + _[0], top + _[1])
                mouse_move_to(left, top)
                time.sleep(SLEEP_TIME)
        _ = Config.Barrack.NumText
        mouse_move_to_and_click(left + _[0], top + _[1])

        clear_and_send_keys(num)

        _ = Config.Barrack.SureButton
        mouse_move_to_and_click(left + _[0], top + _[1])

        time.sleep(SLEEP_TIME)

        # 5. 关闭当前兵营
        _ = Config.Barrack.CloseButton
        mouse_move_to_and_click(left + _[0], top + _[1])


@change_cwd(os.getcwd(), os.path.dirname(os.path.abspath(__file__)))
def auto_make_soldiers(soldier_type=Config.Barrack.QQPositionNum, num=100, double=False):
    time.sleep(10)
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)
    global SLEEP_TIME
    SLEEP_TIME = get_sleep_time(control)
    soldier_cities = get_soldier_cities(control)
    if get_solider_cities_reserve_config(control):
        soldier_cities.reverse()
    for _ in soldier_cities:
        is_first = soldier_cities.index(_) == 0
        send_free_commander(left, top, _[0], _[1])
        make_soldiers(left, top, soldier_type=soldier_type, num=num, double=double, init_solders=is_first)
        # make_soldiers(left, top, num=1000)
        # make_soldiers(left, top, Config.Barrack.ZZPositionNum, num=2000)


def main_city_make_soldiers(soldier_type=Config.Barrack.GJPositionNum, num=1000, double=True):
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)
    global SLEEP_TIME
    SLEEP_TIME = get_sleep_time(control)
    main_make_soldiers(left, top, soldier_type=soldier_type, num=num, double=double)


def soldier_city_superfluous_make(left, top, soldier_type=Config.Barrack.GJPositionNum, num=1000, double=True):
    """
    招兵
        1. 点击多余的兵营
        2. 初始化兵营界面；
        3. 点击轻骑兵的招募按钮；
        4. 输入招募参数并招募；
        5. 关闭当前兵营
    :param left: App的left坐标
    :param top: App的top坐标
    :param soldier_type: 士兵类型
    :param num: 招募数量
    :param double: 是否双倍招募
    :return:
    """
    global SLEEP_TIME
    # 1. 遍历每一座兵营
    _ = (910, 300)
    mouse_move_to_and_click(left + _[0], top + _[1])

    # 2. 初始化兵营界面
    _init_barrack(left, top)
    _ = Config.Barrack.PageDown
    mouse_move_to(left + _[0], top + _[1])
    for _ in range(soldier_type):
        click()

    # 3. 点击轻骑兵的招募按钮
    _ = Config.Barrack.ZhaoMuButton
    mouse_move_to_and_click(left + _[0], top + _[1])

    # 4. 输入招募参数并招募
    if double:
        _ = Config.Barrack.DoubleButton
        # 校验双倍招兵
        while check_double() is False:
            mouse_move_to_and_click(left + _[0], top + _[1])
            mouse_move_to(left, top)
            time.sleep(SLEEP_TIME)
    _ = Config.Barrack.NumText
    mouse_move_to_and_click(left + _[0], top + _[1])

    clear_and_send_keys(num)

    _ = Config.Barrack.SureButton
    mouse_move_to_and_click(left + _[0], top + _[1])

    time.sleep(SLEEP_TIME)

    # 5. 关闭当前兵营
    _ = Config.Barrack.CloseButton
    mouse_move_to_and_click(left + _[0], top + _[1])
    time.sleep(SLEEP_TIME)


def solder_cities_superfluous_make(soldier_type=Config.Barrack.ZZPositionNum, num=1000, double=True):
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)
    soldier_cities = get_soldier_cities(control)
    global SLEEP_TIME
    SLEEP_TIME = get_sleep_time(control)
    for _ in soldier_cities:
        send_free_commander(left, top, _[0], _[1])
        soldier_city_superfluous_make(left, top, soldier_type=soldier_type, num=num, double=double)


if __name__ == "__main__":
    # auto_make_soldiers_city_make_soldiers(soldier_type=Config.Barrack.QQPositionNum, num=2000, double=True)
    # auto_make_soldiers_city_make_soldiers(soldier_type=Config.Barrack.GJPositionNum, num=1000, double=True)
    # auto_make_soldiers(soldier_type=Config.Barrack.QQPositionNum, num=500, double=False)
    auto_make_soldiers(soldier_type=Config.Barrack.QQPositionNum, num=1000, double=False)
    # auto_make_soldiers(soldier_type=Config.Barrack.CNPositionNum, num=500, double=True)
    # auto_make_soldiers(soldier_type=Config.Barrack.TQPositionNum, num=500, double=True)
    # auto_make_soldiers(soldier_type=Config.Barrack.ZZPositionNum, num=1000, double=True)
    # auto_make_soldiers(soldier_type=Config.Barrack.CNPositionNum, num=1000, double=True)
    # auto_make_soldiers(soldier_type=Config.Barrack.GJPositionNum, num=1000, double=False)
    # auto_make_soldiers(soldier_type=Config.Barrack.GJPositionNum, num=2000, double=True)
    # main_city_make_soldiers(soldier_type=Config.Barrack.CHPositionNum, num=100, double=False)
    # main_city_make_soldiers(soldier_type=Config.Barrack.ZZPositionNum, num=500, double=True)
    # main_city_make_soldiers(soldier_type=Config.Barrack.QQPositionNum, num=500, double=False)
    # main_city_make_soldiers(soldier_type=Config.Barrack.NFPositionNum, num=2000, double=False)
    # main_city_make_soldiers(soldier_type=Config.Barrack.GJPositionNum, num=10, double=False)
    # main_city_make_soldiers(soldier_type=Config.Barrack.QQPositionNum, num=1000, double=True)
    # main_city_make_soldiers(soldier_type=Config.Barrack.CQPositionNum, num=2, double=False)
    # main_city_make_soldiers(soldier_type=Config.Barrack.DDPositionNum, num=2, double=False)
    # main_city_make_soldiers()

    # solder_cities_superfluous_make(soldier_type=Config.Barrack.ZZPositionNum, num=1000, double=True)
    # print(check_double())
    # time.sleep(10)
    # control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    # left, top, width, height = get_control_position(control)
    # make_soldiers(left, top, soldier_type=Config.Barrack.GJPositionNum, num=1000, double=True)
    # make_soldiers(left, top, soldier_type=Config.Barrack.TUQIPositionNum, num=1000, double=True)
    # make_soldiers(left, top, soldier_type=Config.Barrack.QQPositionNum, num=1000, double=False)
    # make_soldiers(left, top, soldier_type=Config.Barrack.ZZPositionNum, num=500, double=True)
    # make_soldiers(left, top, soldier_type=Config.Barrack.CNPositionNum, num=1000, double=True, init_solders=True)
